package com.mallcloud.mall.product.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapBuilder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallcloud.mall.common.core.constant.enums.mall.ProductEnum;
import com.mallcloud.mall.product.api.entity.AttrModel;
import com.mallcloud.mall.product.api.vo.AttrExtendVO;
import com.mallcloud.mall.product.mapper.AttrMapper;
import com.mallcloud.mall.product.service.AttrService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 属性信息
 */
@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, AttrModel> implements AttrService {

    @Override
    public Map<String, List<Tree<Long>>> ownerTree(AttrModel dto) {
        Map<String, List<Tree<Long>>> result = MapBuilder.create(new HashMap<String, List<Tree<Long>>>())
                .put(ProductEnum.AttrType.SALE.name(), Collections.emptyList())
                .put(ProductEnum.AttrType.BASE.name(), Collections.emptyList())
                .build();

        List<AttrExtendVO> attrExtends = this.baseMapper.ownerList(dto);
        if (attrExtends.isEmpty()) {
            return result;
        }
        Map<Integer, List<AttrExtendVO>> attrExtendMap = attrExtends.stream().collect(Collectors.groupingBy(AttrExtendVO::getAttrType));
        if (attrExtendMap.containsKey(ProductEnum.AttrType.SALE.getType())) {
            List<AttrExtendVO> attrExtendSaleList = attrExtendMap.get(ProductEnum.AttrType.SALE.getType());
            result.put(ProductEnum.AttrType.SALE.name(), getAttrExtendSaleTree(attrExtendSaleList));
        }
        if (attrExtendMap.containsKey(ProductEnum.AttrType.BASE.getType())) {
            List<AttrExtendVO> attrExtendBaseList = attrExtendMap.get(ProductEnum.AttrType.BASE.getType());
            result.put(ProductEnum.AttrType.BASE.name(), getAttrExtendSaleTree(attrExtendBaseList));
        }
        return result;
    }

    private List<Tree<Long>> getAttrExtendSaleTree(List<AttrExtendVO> attrExtendList) {
        List<TreeNode<Long>> collect = new ArrayList<>();
        long fakeId = attrExtendList.stream().map(AttrExtendVO::getAttrId).collect(Collectors.summarizingLong(x -> x)).getMax() + 1;
        int i = 1;
        List<Long> temp = new ArrayList<>();
        for (AttrExtendVO attrExtend : attrExtendList) {

            if (!temp.contains(attrExtend.getAttrId())) {
                temp.add(attrExtend.getAttrId());

                TreeNode<Long> nodePt = new TreeNode<>();
                nodePt.setId(attrExtend.getAttrId());
                nodePt.setName(attrExtend.getAttrName());
                nodePt.setParentId(0L);
                nodePt.setWeight(i);

                collect.add(nodePt);
                i++;
            }

            TreeNode<Long> nodeCd = new TreeNode<>();
            nodeCd.setId(fakeId);
            nodeCd.setName(attrExtend.getAttrValueName());
            nodeCd.setParentId(attrExtend.getAttrId());
            nodeCd.setWeight(i);
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
            extra.put("factId", attrExtend.getAttrValueId());
            nodeCd.setExtra(extra);

            i++;
            fakeId++;

            collect.add(nodeCd);
        }

        return TreeUtil.build(collect, 0L);
    }
}
